package com.motorhome.manage.service.impl;

import com.motorhome.base.dao.Pager;
import com.motorhome.base.util.FileUtil;
import com.motorhome.base.util.Result;
import com.motorhome.base.util.StringUtil;
import com.motorhome.manage.common.statics.ManageStatics;
import com.motorhome.manage.dao.CarDao;
import com.motorhome.manage.domain.Car;
import com.motorhome.manage.domain.Price;
import com.motorhome.manage.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: John
 */
@Service
public class CarServiceImpl implements CarService {

    @Autowired
    private CarDao carDao;

    @Override
    public Pager<Car> getCarList(String page, String rows) {
        Pager<Car> pager = carDao.getCarList(Integer.parseInt(page), Integer.parseInt(rows));
        if (pager.getItems() != null && !pager.getItems().isEmpty()) {
            List<Long> carIds = pager.getItems().stream().map(Car::getId).collect(Collectors.toList());
            List<Price> prices = carDao.getCarPrice(carIds);
            pager.getItems().forEach(car -> car.setPrices(prices.stream().filter(price -> price.getCarId().equals(car.getId())).collect(Collectors.toList())));
        }
        return pager;
    }

    @Override
    public Result addCar(String name, String description, String status, String remark, MultipartFile file, String viewUrl, String insurance, String carNum,String flag) {
        if (StringUtil.isBlank(name)) {
            return new Result(1, "车辆名称不能为空");
        }
        String picPath = null;
        if (file != null) {
             picPath = new FileUtil(ManageStatics.baseFilePath, "/carpic").upload(file);
        }
        carDao.addCar(name, description, Integer.parseInt(status), remark, picPath, viewUrl, Float.parseFloat(insurance), Integer.parseInt(carNum), Integer.parseInt(flag));
        return new Result(0, "添加成功");
    }

    @Override
    public Result updateCar(String id, String name, String description, String status, String remark, MultipartFile file, String viewUrl, String insurance, String carNum,String flag) {
        if (StringUtil.isBlank(name)) {
            return new Result(1, "车辆名称不能为空");
        }
        String picPath = null;
        if (file != null && !file.isEmpty()) {
            picPath = new FileUtil(ManageStatics.baseFilePath, "/carpic").upload(file);
        }
        carDao.updateCar(Long.parseLong(id), name, description, Integer.parseInt(status), remark, picPath, viewUrl, Float.parseFloat(insurance), Integer.parseInt(carNum),Integer.parseInt(flag));
        return new Result(0, "编辑成功");
    }

    @Override
    public Result deleteCar(String id) {
        if (StringUtil.isBlank(id)) {
            return new Result(1, "车辆ID不能为空");
        }
        carDao.deleteCar(Long.parseLong(id));
        return new Result(0, "删除成功");
    }

    @Override
    public Result addPrice(HttpServletRequest request, String carId, String priceDesc, String price, String orderBy,
                           String rateWeekend, String rate1, String day1, String rate2, String day2, String rate3, String day3) {
        if (StringUtil.isBlank(carId)) {
            return new Result(1, "车辆ID不能为空");
        }
        if (StringUtil.isBlank(priceDesc)) {
            return new Result(1, "价格描述不能为空");
        }
        if (StringUtil.isBlank(price)) {
            return new Result(1, "价格不能为空");
        }

        String topVipRate = request.getParameter("topVipRate");

        carDao.insertPrice(Long.parseLong(carId), priceDesc, Float.parseFloat(price),
                StringUtil.isBlank(orderBy) ? 0 : Integer.parseInt(orderBy),
                Float.parseFloat(rateWeekend),
                Float.parseFloat(rate1), Integer.parseInt(day1),
                Float.parseFloat(rate2), Integer.parseInt(day2),
                Float.parseFloat(rate3), Integer.parseInt(day3),
                topVipRate == null ? null : Float.parseFloat(topVipRate));
        return new Result(0, "添加价格成功");
    }

    @Override
    public Result deletePrice(String priceId) {
        if (StringUtil.isBlank(priceId)) {
            return new Result(1, "价格ID不能为空");
        }
        carDao.deletePrice(Long.parseLong(priceId));
        return new Result(0, "删除价格成功");
    }

    @Override
    public Result bindDevice(String carId, String deviceId, String deviceNumber, String deviceName) {
        if (StringUtil.isBlank(carId)) {
            return new Result(1, "车辆标识必填");
        }
        if (StringUtil.isBlank(deviceId)) {
            return new Result(1, "设备Id必填");
        }
        if (StringUtil.isBlank(deviceNumber)) {
            return new Result(1, "设备编号必填");
        }
        if (StringUtil.isBlank(deviceName)) {
            return new Result(1, "设备名称必填");
        }
        Car car = carDao.getCarByDeviceId(deviceId, deviceNumber);
        if (car != null && car.getId() != Long.parseLong(carId)) {
            return new Result(1, "已有车辆绑定过该设备，如需绑定请先解绑");
        }
        carDao.bindDevice(Long.parseLong(carId), deviceId, deviceNumber, deviceName);
        return new Result(0, "成功");
    }

}
